﻿using CarterWechatServer.BizLogic.Order;
using CarterWechatServer.BizModel.Order;
using Magicodes.Pay.WeChat;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace CarterWechatServer.WebApi.Controllers
{
    /// <summary>
    /// 微信支付回调
    /// </summary>
    public class PayNoifyController : ApiController
    {
        private OrderService _orderService;

        public PayNoifyController()
        {
            _orderService = new OrderService();
        }

        /// <summary>
        /// 支付回调
        /// </summary>
        /// <returns></returns>
        public async Task<HttpResponseMessage> Post()
        {
            Stream stream = HttpContext.Current.Request.InputStream;

            var result = await new WeChatPayApi().PayNotifyHandler(stream, (output, error) =>
             {
                 OrderPayInput input = new OrderPayInput()
                 {
                     OutTradeNo = output.OutTradeNo,
                     TransactionId = output.TransactionId
                 };

                 _orderService.OrderPay(input);
             });

            HttpResponseMessage response = new HttpResponseMessage();

            response.Content = new StringContent(result);
            return response;
        }

    }
}
